<?php
/**
 * GodModelWriter - 为Typecho编辑器添加AI写作和续写功能
 * 
 * @package GodModelWriter
 * @author 陈祖豪（3469807212@qq.com）
 * @version 1.0.0
 * @link https://gitee.com/chenran2006
 */
class AIWriter_Plugin implements Typecho_Plugin_Interface
{
    /**
     * 激活插件方法,如果激活失败,直接抛出异常
     */
    public static function activate()
    {
        // 注入编辑器按钮
        Typecho_Plugin::factory('admin/write-post.php')->bottom = array('AIWriter_Plugin', 'render');
        Typecho_Plugin::factory('admin/write-page.php')->bottom = array('AIWriter_Plugin', 'render');
        
        // 添加路由
        Helper::addRoute('ai-writer', '/ai-writer', 'AIWriter_Action', 'action');

        return _t('插件已启用');
    }

    /**
     * 禁用插件方法,如果禁用失败,直接抛出异常
     */
    public static function deactivate()
    {
        Helper::removeRoute('ai-writer');
        return _t('插件已禁用');
    }

    /**
     * 获取插件配置面板
     */
    public static function config(Typecho_Widget_Helper_Form $form)
    {
        $proxyUrl = new Typecho_Widget_Helper_Form_Element_Text(
            'proxyUrl', 
            NULL, 
            'https://chat.orderease.cn/api/proxy.php',
            _t('天神大模型接口地址'),
            _t('请填写AI接口地址')
        );
        $form->addInput($proxyUrl);
    }

    /**
     * 个人用户的配置面板
     */
    public static function personalConfig(Typecho_Widget_Helper_Form $form){}

    /**
     * 插件实现方法
     */
    public static function render()
    {
        $settings = Helper::options()->plugin('AIWriter');
        $proxyUrl = $settings->proxyUrl;
?>
<script>
window.AIWriterConfig = {
    proxyUrl: '<?php echo $proxyUrl; ?>'
};
</script>
<link rel="stylesheet" href="<?php echo Helper::options()->pluginUrl . '/AIWriter/assets/style.css'; ?>">
<script src="<?php echo Helper::options()->pluginUrl . '/AIWriter/assets/main.js'; ?>"></script>
<?php
    }
} 